package com.alks.function.config;

import com.alks.common.service.RedisService;
import com.alks.entity.data.entity.sys.SysConfLang;
import com.alks.function.service.system.ISysConfLangService;
import org.springframework.context.support.AbstractMessageSource;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Locale;

/**
 * @author: chen
 * @date: 2024/12/24
 * @description: 加载多语言资源
 */
@Component
public class DatabaseMessageSource  extends AbstractMessageSource {

    static final private String CACHE_KEY = "LANG:";

    static final private String CHINESE = "ZH";
    static final private String ENGLISH = "EN";
    static final private String VIETNAMESE = "VNM";

    private final Map<String, MessageFormat> cachedMessageFormats = new HashMap<>();

    @Resource
    private RedisService redisService;

    @Resource
    private ISysConfLangService sysConfLangService;

    @Override
    protected String resolveCodeWithoutArguments(String code, Locale locale) {
        Map<String, String> map = getMessagesMap(locale);
        if (map.containsKey(code)) {
            return map.get(code);
        } else {
            switch (locale.getLanguage().toUpperCase()) {
                case CHINESE:
                    return "未定义编码" + String.format("[%s]", code);
                case ENGLISH:
                    return "Undefined code" + String.format("[%s]", code);
                case VIETNAMESE:
                    return "mã hóa không xác định" + String.format("[%s]", code);
                default:
                    return "Language setting error" + String.format("[%s]", locale.getLanguage());
            }
        }
    }


    private Map<String, String> getMessagesMap(Locale locale) {
        String lang = locale.getLanguage().toUpperCase();
        Object object;
        if (redisService.hasKey(CACHE_KEY + lang)) {
            object = redisService.getCacheMap(CACHE_KEY + lang);
            if (object != null) {
                return (Map<String, String>) object;
            }
        }

        Map<String, String> cnMap = new HashMap<>();
        Map<String, String> enMap = new HashMap<>();
        Map<String, String> vnmMap = new HashMap<>();
        List<SysConfLang> langList = sysConfLangService.listAll();
        for (SysConfLang sysConfLang : langList) {
            cnMap.put(sysConfLang.getCode(), sysConfLang.getZh());
            enMap.put(sysConfLang.getCode(), sysConfLang.getEn());
            vnmMap.put(sysConfLang.getCode(), sysConfLang.getVnm());
        }
        redisService.setCacheMap(CACHE_KEY + CHINESE, cnMap);
        redisService.setCacheMap(CACHE_KEY + ENGLISH, enMap);
        redisService.setCacheMap(CACHE_KEY + VIETNAMESE, vnmMap);

        object = redisService.getCacheMap(CACHE_KEY + lang);
        return (Map<String, String>) object;

    }

    @Override
    protected MessageFormat resolveCode(String code, Locale locale) {
        String msg = getMessagesMap(locale).get(code);
        if (msg == null) {
            return null;
        }
        synchronized (this.cachedMessageFormats) {
            MessageFormat messageFormat = this.cachedMessageFormats.get(locale.getLanguage() + "_" + code);
            if (messageFormat == null) {
                messageFormat = createMessageFormat(msg, locale);
                this.cachedMessageFormats.put(locale.getLanguage() + "_" + code, messageFormat);
            }
            return messageFormat;
        }
    }
}
